package com.lzj.servlet;

import com.lzj.dao.UserDao;
import com.lzj.dao.impl.UserDaoImpl;
import com.lzj.entity.UserInfomation;
import com.lzj.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *
 * </p>
 *
 * @autor:lzj
 * @date:2021/12/7
 */
public class UpdateUserInfServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phoneNumber = req.getParameter("phoneNumber");
        String address = req.getParameter("address");
        String name = req.getParameter("name");
        int userId = (int) req.getSession().getAttribute("userId");
//        System.out.println(phoneNumber);
//        System.out.println(address);
//        System.out.println(name);
//        System.out.println(userId);
        UserServiceImpl userService = new UserServiceImpl();
        UserDao userDao = new UserDaoImpl();
        userService.setUserDao(userDao);
        UserInfomation userInfomation=new UserInfomation(phoneNumber,address,userId);
        int i = userService.updateUserInf(userInfomation);

        UserInfomation userInfomation1 = userService.queryUserInf(userId);//查询库内的全部信息
        req.getSession().setAttribute("userInfomation",userInfomation1);//对session进行重新赋值
        if(i>0){
            if(name.equals("phoneNumber")){
                resp.getWriter().print(phoneNumber);
            }else if (name.equals("address")){
                resp.getWriter().print(address);
            }
        }else{
            resp.getWriter().print("更新失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
